* This do-file assembles outcomes related to pulses adoption, area, and production

*****
* Assemble all the year-season-crop-area data
*****

*** Year 1 Data
tempfile pulse_area

forvalues y = 1/3 {

** ID Data Files
	local fileK "${k`y'}/Farm/areas_inputs_outputs.dta"
	local fileR "${r`y'}/Farm/areas_inputs_outputs.dta"
	local fileZ "${r`y'}/Farm/zaid_mung.dta"

** Kharif 
	use hhid cropcode acres_per_crop_kharif acres_pp_rabi using "`fileK'", clear
	keep if cropcode>=1 & cropcode<=10 		
	//duplicates list hhid cropcode
	//sort hhid cropcode

	* Take Rabi measure if Kharif is missing
	replace acres_per_crop_kharif=acres_pp_rabi if acres_per_crop_kharif==. & cropcode==1
	rename acres_per_crop_kharif pulse_area

	keep hhid cropcode pulse_area
	gen byte year = `y'
	gen byte season = 1

	if `y'>1 append using `pulse_area'
save `pulse_area', replace

** Rabi
	use hhid cropcode acres_per_crop_rabi quantity_production_kg ///
		using "`fileR'", clear
	keep if cropcode>=1 & cropcode<=10
	//duplicates list hhid cropcode
	//sort hhid cropcode

	rename acres_per_crop_rabi pulse_area
	rename quantity_production_kg pulse_prod

	gen byte year = `y'
	gen byte season = 2
	// Assume all pigeon pea is planted in Kharif, so replace Kharif area with area reported in Rabi if farmer reports growing pigeon pea in Rabi but not Kharif; replace Rabi area with missing
	replace season = 1 if cropcode==1

	// update replace: trust Kharif survey for PP area if non-missing
	merge 1:1 hhid year season cropcode using `pulse_area', update replace nogen
save `pulse_area', replace

** Zaid
	use hhid mung_area_acre mung_production_kg using "`fileZ'", clear
	rename mung_area_acre pulse_area
	rename mung_production_kg pulse_prod
	gen byte cropcode = 5
	gen byte year = `y'
	gen byte season = 3
	
	append using `pulse_area'
save `pulse_area', replace
}

*** Fill in zero area planted
merge m:1 hhid using "$admin_data/survey_list.dta", assert(match using) nogen
drop village
	
	* Fill in; add roster variables
	fillin hhid year season 
	drop if missing(year) | missing(season) // fills in missing values as well
	replace crop = 0 if _fillin
	drop _fillin
	merge m:1 hhid using "$out_data/survey_drops.dta", assert(match) nogen keepusing(drop_*)
	
	* Set missing areas to 0 for non-attriters
	recode pulse_area (.=0) if year==1 & season==1
	recode pulse_area (.=0) if year==1 & season==2 & !drop_r1
	recode pulse_area (.=0) if year==1 & season==3 & !drop_k2
	recode pulse_area (.=0) if year==2 & season==1 & !drop_k2
	recode pulse_area (.=0) if year==2 & season==2 & !drop_r2
	recode pulse_area (.=0) if year==2 & season==3 & !drop_r2
	recode pulse_area (.=0) if year==3 & season==1 & !drop_k3
	recode pulse_area (.=0) if year==3 & season==2 & !drop_k3
	recode pulse_area (.=0) if year==3 & season==3 & !drop_r3
	
	* Set missing production to 0 for non-attriters
	recode pulse_prod* (.=0) if year==1 & season==1 & !drop_r1
	recode pulse_prod* (.=0) if year==1 & season==2 & !drop_r1
	recode pulse_prod* (.=0) if year==1 & season==3 & !drop_k2
	recode pulse_prod* (.=0) if year==2 & season==1 & !drop_r2
	recode pulse_prod* (.=0) if year==2 & season==2 & !drop_r2
	recode pulse_prod* (.=0) if year==2 & season==3 & !drop_r2
	recode pulse_prod* (.=0) if year==3 & season==1 & !drop_r3
	recode pulse_prod* (.=0) if year==3 & season==2 & !drop_r3
	recode pulse_prod* (.=0) if year==3 & season==3 & !drop_r3
	
	drop drop_*
	
* Winsorize production and compute yield
winsor pulse_prod, gen(pulse_prod_win5) highonly p(.05)
gen pulse_yield = pulse_prod_win5 / pulse_area
	
order hhid year season cropcode pulse_area pulse_prod* pulse_yield
save "$out_data/outcome_adoption_production.dta", replace
